package com.a.learning_keep.base

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide

abstract class Common<T>(var list: List<T>, var layout: Int) : RecyclerView.Adapter<ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(LayoutInflater.from(parent.context).inflate(layout, parent, false))
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        setBind(holder, list[position], position)
    }

    abstract fun setBind(holder: ViewHolder, data: T, position: Int)

    override fun getItemCount(): Int {
        return list.size
    }

}

class ViewHolder(var item: View) : RecyclerView.ViewHolder(item) {

    fun setText(text: String, id: Int) {
        item.findViewById<TextView>(id).text = text
    }

    fun setLimitImage(img: Int, id: Int) {
        item.findViewById<ImageView>(id).setImageResource(img)
    }

    fun setImage(context: Context, imgUrl: String, id: Int) {
        val iv = item.findViewById<ImageView>(id)
        Glide.with(context).load(Url + imgUrl).into(iv)
    }
}